package com.lswms.service.impl;

import com.lswms.common.page.PageInfo;
import com.lswms.common.page.PageRequest;
import com.lswms.common.service.impl.CrudServiceImpl;
import com.lswms.common.utils.MoneyUtil;
import com.lswms.common.utils.ZqDateUtil;
import com.lswms.dao.YnLotteryRecordDao;
import com.lswms.dto.YnLotteryRecordDTO;
import com.lswms.dto.vo.resp.LotteryListResp;
import com.lswms.entity.YnLotteryRecordEntity;
import com.lswms.entity.YnUserEntity;
import com.lswms.service.YnLotteryRecordService;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import lombok.extern.slf4j.Slf4j;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;

/**
 * 抽奖记录表
 *
 * @author Mark sunlightcs@gmail.com
 * @since 1.0.0 2020-05-06
 */
@Service
@Slf4j
public class YnLotteryRecordServiceImpl extends CrudServiceImpl<YnLotteryRecordDao, YnLotteryRecordEntity, YnLotteryRecordDTO> implements YnLotteryRecordService {

    @Override
    public QueryWrapper<YnLotteryRecordEntity> getWrapper(Map<String, Object> params){
        Long id = (Long)params.get("id");
        Long userId = (Long)params.get("userId");
        String yesnoRead = (String)params.get("yesnoRead");
        String signNo = (String)params.get("signNo");

        QueryWrapper<YnLotteryRecordEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(id != null, "id", id);
        wrapper.eq(userId != null, "user_id", userId);
        wrapper.eq(StringUtils.isNotBlank(yesnoRead), "yesno_read", yesnoRead);
        wrapper.eq(StringUtils.isNotBlank(signNo), "sign_no", signNo);

        return wrapper;
    }
    /**
     * 查询是否参加过本期抽奖
     */
	@Override
	public YnLotteryRecordEntity queryLottery(YnUserEntity ynuser) {
		String yyyyMMdd = ZqDateUtil.yyyyMMdd(ZqDateUtil.getNow());
		//查询期数
		QueryWrapper<YnLotteryRecordEntity> wrapper = new QueryWrapper<YnLotteryRecordEntity>();
		wrapper.eq(StringUtils.isNotBlank(yyyyMMdd), "sign_no", yyyyMMdd);
		wrapper.eq("user_id", ynuser.getId());
		YnLotteryRecordEntity lottery = baseDao.selectOne(wrapper);
		if(lottery == null){
			return null;
		}
		return lottery;
	}
	/**
	 * 抽奖记录
	 */
	@Override
	public PageInfo<LotteryListResp> lotteryList(PageRequest dto,
			YnUserEntity ynuser) {
		log.info("抽奖记录，参数dto:{},ynuser:{}",JSON.toJSONString(dto),JSON.toJSONString(ynuser));
		//更新抽奖记录已读
		baseDao.updateYesnoRead(ynuser.getId());
        QueryWrapper<YnLotteryRecordEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", ynuser.getId());
        
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("page", dto.getPageNo());
        params.put("limit", dto.getPageSize());

		IPage<YnLotteryRecordEntity> pageInfo = baseDao.selectPage(getPage(params, "create_date", false),wrapper);
		
		List<LotteryListResp> list = new ArrayList<LotteryListResp>();
		for(YnLotteryRecordEntity entity : pageInfo.getRecords()){
			LotteryListResp resp = new LotteryListResp();
			resp.setId(entity.getId());
			resp.setSignNo(entity.getSignNo());
			//未开奖
			if(StringUtils.isBlank(entity.getLotteryLevel())){
				resp.setStatus("0");
				resp.setAmount("");
		    	//开奖时间（第二天）
		    	String signTime = ZqDateUtil.yyyy_MM_dd(ZqDateUtil.addDate(entity.getCreateDate(), 1, Calendar.DAY_OF_MONTH));
				resp.setOpenTime(signTime+" 10:00:00");
			}else{
				resp.setStatus("1");
				resp.setAmount(MoneyUtil.bigDecimal2String(entity.getAmount(), 2));
				resp.setOpenTime(ZqDateUtil.yyyyMMddHHmmss(entity.getUpdateDate()));
			}
			list.add(resp);
		}
		
		PageInfo<LotteryListResp> pageList = new PageInfo<LotteryListResp>();
		pageList.setCount(pageInfo.getTotal());
		pageList.setList(list);
		pageList.setPageNo(Long.parseLong(dto.getPageNo()));
		pageList.setPages(pageInfo.getPages());
		pageList.setPageSize(Long.parseLong(dto.getPageSize()));
		log.info("抽奖记录，结果:{}",JSON.toJSONString(pageList));
		return pageList;
	}
}